<?php

require_once ('youtubevideostatistic.php');

require_once ('youtubevideocomment.php');


class YoutubeVideo{
	var $id;
	var $link;
	var $title;
	var $content;
	var $published;
	var $updated;
	var $author;
	var $information;
	var $statistic;
	var $categories;
	//customization
	var $width = 425;
	var $height = 350;	

	//internals
	var $m_swfObject = false;

	function shortId(){
		$url = parse_url ( $this->id );
		$split = explode ("/", $url['path'] );
		$lastKey = count($split) - 1;
		return $split[$lastKey];
	}

	/**
	*	 @param includeJavascript include inline javascript
	*	 @return flash player
	*	 @see setHasSwfObject
	*/
	function flashPlayer( $includeJavascript = false ){
		if ( !$this->m_swfObject ){
			return "
			<object width=\"{$this->width}\" height=\"{$this->height}\">
			<param name=\"movie\" value=\"{$this->information->mediaUrl}\"></param>
			<embed src=\"{$this->information->mediaUrl}\"
			type=\"{$this->information->mediaType}\" width=\"{$this->width}\" height=\"{$this->height}\">
			</embed>
			</object>";
		} else{
			return $this->swfObjectDiv( $includeJavascript );
		}

	}
	/**
	* @param includeJavascript include inline javascript
	* @return a div to replace with swfobject
	*
	*/
	function swfObjectDiv( $includeJavascript = false ){
		$debug = null;
		if ( DEBUG ) $debug = '<p>You must call swfObjectJavascript() method to replace this text with the flash object</p>';
		else $debug = '<p>Loading...</p>';
		$ret = "<div id=\"{$this->id}\" class=\"flashPlayer\">$debug</div>";
		if ( $includeJavascript ) $ret = "\n ".$this->swfObjectJavascript();
		return $ret;
	}
	/**
	*	@param tags include or not the <script></script> tags
	* 	@return javascript for the current video
	*
	*/
	function swfObjectJavascript( $tags = true ){
		if ( $tags )
			return "<script type=\"text/javascript\">
				swfobject.embedSWF(\"{$this->information->mediaUrl}\", \"{$this->id}\", \"{$this->width}\", \"{$this->height}\", \"9.0.0\");
			</script>
			";
		else 
			return "swfobject.embedSWF(\"{$this->information->mediaUrl}\", \"{$this->id}\", \"{$this->width}\", \"{$this->height}\", \"9.0.0\");";
	}

	/**
	*
	* Use to activate or desactivate swfobject javascript
	*
	*/

	function setHasSwfObject( $b ){
		$this->m_swfObject = $b;
	}

	function __toString(){
		return $this->flashPlayer();
	}
	
	/**
	*
	* @return the media url for id @param id
	*
	*/
	function mediaUrlForId( $id ){
		return "http://www.youtube.com/v/$id&f=gdata_standard";
	}
	
	/**
	*
	* @return the media url for the current id
	*
	*/

	function mediaUrlFromId(){
		return $this->mediaUrlForId( $this->shortId() );
	}
	
	/** 
	*
	* @return commments for the current video video
	* TODO: improve 
	*/
	function comments ( $options = array() ){
		$requestObject = new Youtube;
		$requestObject->_setUrl( $this->statistic->commentsFeed );
		$parser = new YoutubeVideoCommentParser( $requestObject->_request() );
		return $parser->comments;
		
	}

} 

?>